from django.http import JsonResponse
import json


class custom_response():
    def __init__(self):
        self.token = ''
        self.resp = {'code': 0, 'msg': '操作成功', 'data': {}}
        self.__data = {}

    def set_code(self, code):
        self.resp['code'] = code

    def data_update(self, dict_params={}, **kwargs):
        if dict_params:
            self.__data = dict_params
        else:
            self.__data.update(kwargs)
        self.resp['data'] = self.__data

    def set_message(self, error=''):
        if error:
            self.resp['msg'] = error

    def set_token(self, token):
        self.token = token

    @property
    def response(self):
        response = JsonResponse(self.resp)
        if self.token:
            response['Oauth-Token'] = self.token
        return response
